class GeozoneTriangleController {

    def index = {redirect(action: list, params: params)}

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [geozoneTriangleList: GeozoneTriangle.list(params)]
    }

    def show = {
        def geozoneTriangle = GeozoneTriangle.get(params.id)

        if (!geozoneTriangle) {
            flash.message = "GeozoneTriangle not found with id ${params.id}"
            redirect(action: list)
        }
        else {return [geozoneTriangle: geozoneTriangle]}
    }

    def delete = {
        def geozoneTriangle = GeozoneTriangle.get(params.id)
        if (geozoneTriangle) {
            geozoneTriangle.delete()
            flash.message = "GeozoneTriangle ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = "GeozoneTriangle not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def edit = {
        def geozoneTriangle = GeozoneTriangle.get(params.id)

        if (!geozoneTriangle) {
            flash.message = "GeozoneTriangle not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            return [geozoneTriangle: geozoneTriangle]
        }
    }

    def update = {
        def geozoneTriangle = GeozoneTriangle.get(params.id)
        if (geozoneTriangle) {
            geozoneTriangle.properties = params
            if (!geozoneTriangle.hasErrors() && geozoneTriangle.save()) {
                flash.message = "GeozoneTriangle ${params.id} updated"
                redirect(action: show, id: geozoneTriangle.id)
            }
            else {
                render(view: 'edit', model: [geozoneTriangle: geozoneTriangle])
            }
        }
        else {
            flash.message = "GeozoneTriangle not found with id ${params.id}"
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def geozoneTriangle = new GeozoneTriangle()
        geozoneTriangle.properties = params
        return ['geozoneTriangle': geozoneTriangle]
    }

    def save = {
        def geozoneTriangle = new GeozoneTriangle(params)

        def lat1 = params.latitude1
        def lng1 = params.longitude1
        def lat2 = params.latitude2
        def lng2 = params.longitude2
        def lat3 = params.latitude3
        def lng3 = params.longitude3

        def p1 = new Point("latitude": lat1, "longitude": lng1, "zoneType": "Triangle")
        def p2 = new Point("latitude": lat2, "longitude": lng2, "zoneType": "Triangle")
        def p3 = new Point("latitude": lat3, "longitude": lng3, "zoneType": "Triangle")
        geozoneTriangle.addToPoints(p1).addToPoints(p2).addToPoints(p3)

        if (!geozoneTriangle.hasErrors() && geozoneTriangle.save()) {
            flash.message = "GeozoneTriangle ${geozoneTriangle.id} created"
            redirect(action: show, id: geozoneTriangle.id)
        }
        else {
            render(view: 'create', model: [geozoneTriangle: geozoneTriangle])
        }
    }
}